<?php

$thisPage = "list_exportproducts";

$params = getRequestParameter("params");
$params[strlen($params)-5] = ' ';
$params[strlen($params)-6] = ' ';
$params = unserialize(stripslashes(getRequestParameter("params")));

if (isset($params['freetext'])) { $params['freetext'] = urldecode($params['freetext']); }
if (isset($params['enteredText'])) { $params['enteredText'] = urldecode($params['enteredText']); }

if (!isset($params) || $params == "") {
    $params = $_POST;
}

$pageinfo = $params['pageinfo'];

loadJQueryPlugin("innerfade");
loadJQueryPlugin("hsinput");
loadJQueryPlugin("freetextsearch");
loadJQueryPlugin("imghover");

loadPageJavascripts("exportproducts");

TemplateHelper::useLayout("list");

addDataservice("getpage");
addHtmlService("getpage");

Log::info("Loading page functions: $thisPage");

function executeService($serviceName, $params) {
    switch ($serviceName) {
        case "getpage" : { return execute_getpage(); break; }
    }
}

function buildLayout(&$smarty) {
    global $thisPage;
    global $params;
    Log::debug("Executing function: buildLayout");

    Log::debug("storing last search");
    storeParameter("lastsearch", $params);
    $result = execute_getresult($params);

    initDatabase();
    Database::loadModel("companies");
    $companies = Database::getModel()->getRotatorCompaniesFor("export");
    $pictures = array(
        array(src => "site/layout/searchscreen/resources/img/gop.png", href => "index.php?choice=gop"),
        array(src => "site/layout/searchscreen/resources/img/youcv_banner_2.jpg", href => "http://www.lancasterconsulting.hu"),
        array(src => "site/layout/searchscreen/resources/img/budapest_banner.jpg", href => "http://www.exporthungary.hu"),
        array(src => "site/layout/searchscreen/resources/img/empty_rotator.png", href => "index.php?choice=mediaoffer"),
    );
    $counter = 0;
    for ($i = 0; $i < count($companies) && $i < 4; $i++) {
        if ($i == 1) { $counter++; }
        $pictures[$counter]['src'] = callFileloaderDisplayCompanyFile("export", "rotator", $companies[$i]['company_id']);
        $pictures[$counter]['href'] = $companies[$i]['hphungarian'];
        $counter++;
        if ($counter >= 4) { break; }
    }
    $smarty->assign("virt_exhibition", true);
    $smarty->assign("searchvalue", urldecode($params['enteredText']));
    $smarty->assign("pictures", $pictures);
    $smarty->assign("activesearchtab", "export_products");
    $smarty->assign("searchcontent", $smarty->fetch(TemplateHelper::getTemplateFile("exportproducts", "ep_search")));
    $smarty->assign("list", array(
        hassummaryrow => true,
        summaryrow => getsummary($smarty, $result['resultCount']),
        pager => getpager($result['resultCount']),
        content => getcontent($smarty, $result),
        title => "Hungarian exporters   ",
    ));
    $smarty->assign("pager", true);
}

function execute_getresult($params) {
    global $pageinfo;

    initDatabase();
    Database::loadModel("companies");
    $currentPage = ($pageinfo['current_page'] ? $pageinfo['current_page'] : 1);

    $result = array();
    if (isset($params['freetext'])) {
        $count = Database::getModel()->getExportCompaniesCountByFreetext($params['freetext']);
        $result = Database::getModel()->getExportCompaniesByFreetext($currentPage, $params['freetext']);
    } else if (isset($params['selectedItem'])) {
        $count = 1;
        $result = array(
            Database::getItem($params['selectedItem'])
        );
        $result[0] = array_merge($result[0], Database::getModel()->getExportCompanyProducts($params['selectedItem']));
    } else if (isset($params['selectedName'])) {
        $count = 1;
        $result = array(
            Database::getitemByCompanyName($params['selectedName'])
        );
        $result[0] = array_merge($result[0], Database::getModel()->getExportCompanyProducts($result[0]['company_id']));
    } else {
        $count = Database::getModel()->getExportCompaniesCount($params['hs2'], $params['hs4']);
        $result = Database::getModel()->getExportCompanies($currentPage, $params['hs2'], $params['hs4']);
    }
    for ($i = 0; $i < count($result); $i++) {
        if (strlen($result[$i]['freetype_text']) < 120) { continue; }
        $cut = strpos($result[$i]['freetype_text'], " ", 120);
        if ($cut !== false) {
            $result[$i]['freetype_text'] = substr($result[$i]['freetype_text'], 0, $cut) . "...";
        }
    }
    return array(
        result => $result,
        resultCount => $count,
    );
}

function execute_getpage() {
    global $pageinfo;
    global $smarty;

    $params = getStoredParameter("lastsearch");
    $result = execute_getresult($params);
    return getContent($smarty, $result);

}

function getSummary($smarty, $resultCount) {
    global $params;
    global $thisPage;
    if ($resultCount == 0) { return ""; }
    $smarty->assign("summary", array(
        count => $resultCount,
        freetype => ($params['hs2'] ? false : true),
        hs4code => $params['hs4'],
        productname => $params['freetext'],
    ));
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "summary"));
}

function getpager($resultCount) {
    global $pageinfo;
    if ($resultCount == 0) { return ""; }
    return array(
        currentpage => ($pageinfo['current_page'] ? $pageinfo['current_page'] : 1),
        pagecount => ceil($resultCount / 10),
    );
}

function getContent($smarty, $result) {
    global $thisPage;
    $resultHTML = "";
    $counter = 1;
    if (count($result['result']) == 0) {
        $resultHTML = $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "noresult"));
    } else {
        for ($i = 0; $i < count($result['result']); $i++) {
            if ($i % 5 == 0 && $i > 0) {
                $resultHTML .= $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "ad"));
            }
            if ($counter % 4 == 0 && $i > 0) {
                $smarty->assign("usedefaultimg", true);
            } else {
                $smarty->assign("usedefaultimg", false);
            }
            $smarty->assign("item", $result['result'][$i]);
            $resultHTML .= $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "contentitem"));
            $counter++;
        }
    }
    return $resultHTML;
}

function getPageHeader() {
    return TemplateHelper::getDefaultPageHeader();
}

function getPageFooter() {
    return TemplateHelper::getDefaultPageFooter(array(
        type => "simple"
    ));
}

function getLeftSide($smarty) {
    global $thisPage;
    global $_CONFIG;

    $smarty->assign("leftpictures", array(
    ));
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "leftpictures"));
}

function getTop($smarty) {
    global $thisPage;

    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "toppictures"));
}

function getBottom($smarty) {
    global $thisPage;
    
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "bottom"));
}
